﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;
using Think.Lenovo.Com.Models.Material;
using Think.Lenovo.Com.Models;
using System.Reflection;
using Think.Lenovo.Com.DataAccess;

namespace Think.Lenovo.Com.home
{
    /// <summary>
    /// 产品频道页
    /// </summary>
    public partial class productChannel : System.Web.UI.Page
    {
        productClassInfo classInfo = new productClassInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindData();
                BindMenu();
                BindPad();
                BindOption();
                BindPlus();

                Panel2.Visible = false;
                Panel3.Visible = false;
            }
        }



        #region 左边菜单
        public void BindMenu()
        {
            DataSet ds = classInfo.GetSubClassList("0");
            Repeater2.DataSource = DataSetToIList<MaterialModel>(ds, "tbl_product_class");
            Repeater2.DataBind();
        }

        protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
            {
                MaterialModel nm = (MaterialModel)e.Item.DataItem;

                HyperLink lButton = (HyperLink)e.Item.FindControl("HyperLink2");
                lButton.Text = nm.ClassName.ToString();
                lButton.NavigateUrl = "/home/product.aspx";
               // lButton.Attributes.Add("onclick", "getChildClass('" + nm.ClassId + "')");
            }
        }
        #endregion

        #region 最新产品

        public void BindPad()
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            string sql = "Select tbl_product.ID,tbl_product.Title, tbl_product.Pic,tbl_product.Content,tbl_product.Bright,tbl_product.ReleaseTime from tbl_product join tbl_product_class on tbl_product_class.ClassId=tbl_product.Pclass and tbl_product_class.ClassList like '%201108081837310625%' order by tbl_product.ID desc limit  0,8";
           
            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Repeater_pad.DataSource = DataSetToIList<productModel>(ds, "tbl_product");
            Repeater_pad.DataBind();
        }

        protected void Repeater_pad_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
            {
                productModel nm = (productModel)e.Item.DataItem;

                HyperLink lButton = (HyperLink)e.Item.FindControl("HyperLink3");
                lButton.NavigateUrl = "/home/productView.aspx?id=" + nm.ID;

                Image img = (Image)e.Item.FindControl("Image2");
                img.ImageUrl = nm.Pic;

                HyperLink title = (HyperLink)e.Item.FindControl("HyperLink4");
                title.NavigateUrl = "/home/productView.aspx?id=" + nm.ID;
                title.Text = nm.Title;
            }
        }


        public void BindOption()
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            string sql = "Select tbl_product.ID,tbl_product.Title, tbl_product.Pic,tbl_product.Content,tbl_product.Bright,tbl_product.ReleaseTime from tbl_product join tbl_product_class on tbl_product_class.ClassId=tbl_product.Pclass and tbl_product_class.ClassList like '%201108081849587187%' order by tbl_product.ID desc limit  0,8";
            



            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Repeater_Option.DataSource = DataSetToIList<productModel>(ds, "tbl_product");
            Repeater_Option.DataBind();
        }

        protected void Repeater_Option_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
            {
                productModel nm = (productModel)e.Item.DataItem;

                HyperLink lButton = (HyperLink)e.Item.FindControl("HyperLink5");
                lButton.NavigateUrl = "/home/productView.aspx?id=" + nm.ID;

                Image img = (Image)e.Item.FindControl("Image3");
                img.ImageUrl = nm.Pic;

                HyperLink title = (HyperLink)e.Item.FindControl("HyperLink6");
                title.NavigateUrl = "/home/productView.aspx?id=" + nm.ID;
                title.Text = nm.Title;


            }
        }


        public void BindPlus()
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            string sql = "Select tbl_product.ID,tbl_product.Title, tbl_product.Pic,tbl_product.Content,tbl_product.Bright,tbl_product.ReleaseTime from tbl_product join tbl_product_class on tbl_product_class.ClassId=tbl_product.Pclass and tbl_product_class.ClassList like '%201108081850101406%' order by tbl_product.ID desc limit  0,8";
            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Repeater_Plus.DataSource = DataSetToIList<productModel>(ds, "tbl_product");
            Repeater_Plus.DataBind();
        }

        protected void Repeater_Plus_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
            {
                productModel nm = (productModel)e.Item.DataItem;

                HyperLink lButton = (HyperLink)e.Item.FindControl("HyperLink7");
                lButton.NavigateUrl = "/home/productView.aspx?id=" + nm.ID;

                Image img = (Image)e.Item.FindControl("Image4");
                img.ImageUrl = nm.Pic;

                HyperLink title = (HyperLink)e.Item.FindControl("HyperLink8");
                title.NavigateUrl = "/home/productView.aspx?id=" + nm.ID;
                title.Text = nm.Title;
            }
        }

        #endregion

        #region dataset to ilist
        public IList<T> DataSetToIList<T>(DataSet p_DataSet, string p_TableName)
        {

            int _TableIndex = 0;

            if (p_DataSet == null || p_DataSet.Tables.Count < 0)

                return null;

            if (string.IsNullOrEmpty(p_TableName))

                return null;

            for (int i = 0; i < p_DataSet.Tables.Count; i++)
            {

                // 获取Table名称在Tables集合中的索引值   

                if (p_DataSet.Tables[i].TableName.Equals(p_TableName))
                {

                    _TableIndex = i;

                    break;

                }

            }
            return DataSetToIList<T>(p_DataSet, _TableIndex);

        }

        public IList<T> DataSetToIList<T>(DataSet p_DataSet, int p_TableIndex)
        {

            if (p_DataSet == null || p_DataSet.Tables.Count < 0)

                return null;

            if (p_TableIndex > p_DataSet.Tables.Count - 1)

                return null;

            if (p_TableIndex < 0)
                p_TableIndex = 0;


            DataTable p_Data = p_DataSet.Tables[p_TableIndex];

            // 返回值初始化   

            IList<T> result = new List<T>();
            for (int j = 0; j < p_Data.Rows.Count; j++)
            {

                T _t = (T)Activator.CreateInstance(typeof(T));
                PropertyInfo[] propertys = _t.GetType().GetProperties();

                foreach (PropertyInfo pi in propertys)
                {
                    for (int i = 0; i < p_Data.Columns.Count; i++)
                    {

                        // 属性与字段名称一致的进行赋值   
                        if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
                        {
                            // 数据库NULL值单独处理   
                            if (p_Data.Rows[j][i] != DBNull.Value)

                                pi.SetValue(_t, p_Data.Rows[j][i], null);
                            else

                                pi.SetValue(_t, null, null);
                            break;

                        }

                    }

                }

                result.Add(_t);

            }

            return result;
        }

        #endregion

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
            Panel3.Visible = false;
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            Panel3.Visible = false;
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = true;
        }


    }
}